import pymongo

def open_mongodb():
    myclient = pymongo.MongoClient('localhost',27017)
    db = myclient['radarssd']
    collection = db['radarssd']

    return collection

def convert_dict(TOA, DOA, RF):
    return {'TOA':TOA, 'DOA':DOA, 'RF':RF}

#插入一个数组（即使只有一个，也以数组的形式插入）
def insert_mongodb(collection, signal_dict_list):
    collection.insert_many(signal_dict_list)

def get_toa(collection, start_toa, toa_length):
    rets = collection.find({'TOA':{'$gt':start_toa,'$lt':start_toa+toa_length+100}},{"_id": 0, "TOA":1}).limit(toa_length)
    TOA = []
    for ret in rets:
        TOA.append(ret['TOA'])
    return TOA

def get_total_data(collection):
    return collection.count_documents({})

def clear_mongodb(collection):
    collection.drop()

if __name__ == "__main__":
    fd = open_mongodb()

    # signal_list = []
    # for i in range(100):
    #     signal_list.append(convert_dict(i, i+1, i+2))
    # insert_mongodb(fd, signal_list)
    # print("Insert data")

    print("Create Index for TOA")
    fd.create_index('TOA', name='TOA_index')
    print("Create Index done")

    # print("Clear Index for TOA")
    # fd.drop_index('TOA_index')
    # print("Clear Index done")

    #print(get_total_data(fd))

    #print(get_toa(fd, 5, 100))

    #clear_mongodb(fd)

    #print(get_toa(fd, 5, 100))
    